package org.zappee.dannyt.cairngorm.service
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class CommonMockDelegate
	{
		public function CommonMockDelegate(responder : IResponder)
		{
			this.responder = responder;
		}
		
		protected function setResults(results:Object):void
		{
			this.results = results;
			if(latency>0)
			{
				this.timer = new Timer(latency);
				timer.addEventListener("timer",invokeResponder);			
				timer.start();
			}
			else
			{
				invokeResponder(null);
			}
		}
		
		private function invokeResponder(evt:TimerEvent):void{
			
			try
			{
				if(evt!=null) timer.stop();
				var ev:ResultEvent = new ResultEvent("MockUp",false,true,results);
				responder.result(ev);
			}
			catch(er:Error)
			{
				responder.fault(new FaultEvent("MockUp"));
			}			
		}
		
		protected function set Latency(time:Number):void{
			this.latency = time;
		}
		
		private var responder : IResponder;
		//using a timer to simulate the latency during the client-server roundtrip
		private var timer:Timer;
		//the latency value can be set by the specific Business Delegates
		private var latency:Number = 0;  //the default value is no latency
		//holding the results in a global is a little awkward...
		private var results:Object;

	}
}